<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="会员ID" prop="memberId">
        <el-input
          v-model="queryParams.memberId"
          placeholder="请输入会员ID"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="钱包状态" prop="walletState">
        <el-input
          v-model="queryParams.walletState"
          placeholder="请输入钱包状态"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['uac:member-wallet:export']"
        >导出
        </el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="walletList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center"/>
      <el-table-column label="会员用户名" align="center" prop="memberName"/>
      <el-table-column label="总金额" align="center" prop="totalAmount"/>
      <el-table-column label="冻结金额" align="center" prop="frozenAmount"/>
      <el-table-column label="可用金额" align="center" prop="avaAmount"/>
      <el-table-column label="钱包状态" align="center" prop="walletState">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.uac_member_wallet_state" :value="scope.row.walletState"/>
        </template>
      </el-table-column>
      <el-table-column label="备注" align="center" prop="remark"/>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['uac:member-wallet:edit']"
          >修改
          </el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-s-order"
            @click="handleBill(scope.row)"
            v-hasPermi="['uac:member-wallet-bill:list']"
          >账单
          </el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改会员钱包对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="会员ID" prop="memberId">
          <el-input v-model="form.memberId" placeholder="请输入会员ID"/>
        </el-form-item>
        <el-form-item label="会员用户名" prop="memberName">
          <el-input v-model="form.memberName" placeholder="请输入会员用户名"/>
        </el-form-item>
        <el-form-item label="总金额" prop="totalAmount">
          <el-input v-model="form.totalAmount" placeholder="请输入总金额"/>
        </el-form-item>
        <el-form-item label="冻结金额" prop="frozenAmount">
          <el-input v-model="form.frozenAmount" placeholder="请输入冻结金额"/>
        </el-form-item>
        <el-form-item label="可用金额" prop="avaAmount">
          <el-input v-model="form.avaAmount" placeholder="请输入可用金额"/>
        </el-form-item>
        <el-form-item label="钱包密码" prop="walletPassword">
          <el-input v-model="form.walletPassword" placeholder="请输入钱包密码"/>
        </el-form-item>
        <el-form-item label="钱包状态 0 未启用  1 正常  2 冻结" prop="walletState">
          <el-input v-model="form.walletState" placeholder="请输入钱包状态 0 未启用  1 正常  2 冻结"/>
        </el-form-item>
        <el-form-item label="删除标志 0 未删除  1 已删除" prop="deleteFlag">
          <el-select v-model="form.deleteFlag" placeholder="请选择删除标志 0 未删除  1 已删除">
            <el-option
              v-for="dict in dict.type.sys_is_enable"
              :key="dict.value"
              :label="dict.label"
              :value="parseInt(dict.value)"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="备注" prop="remark">
          <el-input v-model="form.remark" placeholder="请输入备注"/>
        </el-form-item>
        <el-form-item label="版本号" prop="rversion">
          <el-input v-model="form.rversion" placeholder="请输入版本号"/>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
    <!-- 会员钱包账单弹出层   -->
    <el-dialog title="账单流水" :visible.sync="billOpen" width="70%" append-to-body>
      <member-wallet-bill :memberId="memberId"></member-wallet-bill>
      <div slot="footer" class="dialog-footer">
        <el-button @click="billOpen = false">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {listWallet, getWallet,  addWallet, updateWallet} from "@/api/uac/memberWallet";
import MemberWalletBill from './bill-list.vue'

export default {
  name: "MemberWallet",
  dicts: ['sys_is_enable', 'uac_member_wallet_state'],
  components: {
    MemberWalletBill
  },
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 会员钱包表格数据
      walletList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 是否显示账单弹出层
      billOpen: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        memberId: null,
        memberName: null,
        totalAmount: null,
        frozenAmount: null,
        avaAmount: null,
        walletPassword: null,
        walletState: null,
        deleteFlag: null,
        rversion: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {},
      memberId: null
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询会员钱包
     列表 */
    getList() {
      this.loading = true;
      listWallet(this.queryParams).then(response => {
        this.walletList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        memberId: null,
        memberName: null,
        totalAmount: null,
        frozenAmount: null,
        avaAmount: null,
        walletPassword: null,
        walletState: null,
        deleteFlag: null,
        remark: null,
        rversion: null,
        createBy: null,
        createTime: null,
        updateBy: null,
        updateTime: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length !== 1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加会员钱包";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids
      getWallet(id).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改会员钱包";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            updateWallet(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addWallet(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('member/wallet/export', {
        ...this.queryParams
      }, `wallet_${new Date().getTime()}.xlsx`)
    },
    /** 账单按钮操作 */
    handleBill(row) {
      this.memberId = row.memberId;
      this.billOpen = true;
    },
  }
};
</script>
